package com.chinatgl.latte.ec.main.sort.list;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;
import com.chinatgl.latte.delegates.LatteDelegate;
import com.chinatgl.latte.ec.API;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.R2;
import com.chinatgl.latte.ec.main.sort.SortDelegate;
import com.chinatgl.latte.ec.main.sort.content.ContentDelegate;
import com.chinatgl.latte.ec.main.sort.content.SectionBean;
import com.chinatgl.latte.ec.params.MapUtil;
import com.chinatgl.latte.net.NetRequest;
import com.chinatgl.latte.net.listener.onNetRquestListener;
import com.chinatgl.latte.util.json.Result;
import com.google.gson.reflect.TypeToken;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportHelper;

/**
 * 文件描述: 垂直的列表
 * 作者: Created by 杨伟乔 on 2017/8/10 0010.
 * 版本号: 1.0
 */

public class VerticalListDelegate extends LatteDelegate {

    @BindView(R2.id.rv_vertical_menu_list)
    RecyclerView mRecyclerView = null;
    private List<SectionBean> data;
    private SortRecyclerAdapter adapter;
    private int oldposition = 0;

    @Override
    public Object setLayout() {
        return R.layout.delegate_vertical_list;
    }

    private void initRecyclerView() {
        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(manager);
        //屏蔽动画效果
        mRecyclerView.setItemAnimator(null);
        mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                adapter.setSelectPos(i);
                adapter.notifyDataSetChanged();
                //处理右侧的变化
                String id = data.get(i).getId();
                showContent(id);
            }

            @Override
            public void onItemLongClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemChildLongClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }
        });


    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initRecyclerView();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        WeakHashMap<String, Object> params = MapUtil.getParams();
        Type type = new TypeToken<Result<ArrayList<SectionBean>>>() {
        }.getType();
        NetRequest.post(API.TOPCATLIST, params, type, true, 60 * 24, new onNetRquestListener() {
            @Override
            public void requestdataFinish(String catalog, Object bean) {


                if (bean != null) {
                    Result<ArrayList<SectionBean>> result = (Result<ArrayList<SectionBean>>) bean;
                    if (result.getCode() == 0) {
                        data = result.getResult();
                        DELEGATE = getParentDelegate();
                        adapter = new SortRecyclerAdapter(data);
                        mRecyclerView.setAdapter(adapter);
                        String id = data.get(0).getId();
                        showContent(id);
                    }

                }


            }

            @Override
            public void requestdataFailed(String msg, Throwable e) {

            }

            @Override
            public void requestComplete() {

            }
        },getActivity());
    }


    private void showContent(String contentId) {
        final ContentDelegate delegate = ContentDelegate.newInstance(contentId);
        switchContent(delegate);
    }

    private SortDelegate DELEGATE;

    private void switchContent(ContentDelegate delegate) {
        final LatteDelegate contentDelegate =
                SupportHelper.findFragment(DELEGATE.getChildFragmentManager(), ContentDelegate.class);
        if (contentDelegate != null) {
            contentDelegate.getSupportDelegate().replaceFragment(delegate, false);
        }
    }

}
